﻿using System;
using System.Collections.Generic;
using System.Data;

namespace SPDbFileExtractor.Framework.Entities
{
    /// <summary>
    /// ListItem
    /// </summary>
    public class ListItem
    {
        /// <summary>
        /// Gets or sets the file id.
        /// </summary>
        /// <value>
        /// The file id.
        /// </value>
        public Guid FileId { get; set; }

        /// <summary>
        /// Gets or sets the web id.
        /// </summary>
        /// <value>
        /// The web id.
        /// </value>
        public Guid WebId { get; set; }

        /// <summary>
        /// Gets or sets the site id.
        /// </summary>
        /// <value>
        /// The site id.
        /// </value>
        public Guid SiteId { get; set; }

        /// <summary>
        /// Gets or sets the name of the dir.
        /// </summary>
        /// <value>
        /// The name of the dir.
        /// </value>
        public string DirName { get; set; }

        /// <summary>
        /// Gets or sets the name of the file.
        /// </summary>
        /// <value>
        /// The name of the file.
        /// </value>
        public string FileName { get; set; }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>
        /// The size.
        /// </value>
        public int Size { get; set; }

        /// <summary>
        /// Gets or sets the UI version.
        /// </summary>
        /// <value>
        /// The UI version.
        /// </value>
        public int UIVersion { get; set; }

        /// <summary>
        /// Gets or sets the internal version.
        /// </summary>
        /// <value>
        /// The internal version.
        /// </value>
        public int InternalVersion { get; set; }

        /// <summary>
        /// Gets or sets the E tag version.
        /// </summary>
        /// <value>
        /// The E tag version.
        /// </value>
        public int ETagVersion { get; set; }

        /// <summary>
        /// Gets or sets the created.
        /// </summary>
        /// <value>
        /// The created.
        /// </value>
        public DateTime Created { get; set; }

        /// <summary>
        /// Gets or sets the created by.
        /// </summary>
        /// <value>
        /// The created by.
        /// </value>
        public string CreatedBy { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            if (DirName == null || FileName == null)
            {
                return string.Empty;
            }

            return string.Format("{0}/{1}", DirName, FileName);
        }

        /// <summary>
        /// Gets the collection from table.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static List<ListItem> GetCollectionFromTable(DataTable table)
        {
            if (table == null || table.Rows.Count == 0)
            {
                return new List<ListItem>();
            }

            var result = new List<ListItem>();

            foreach (DataRow row in table.Rows)
            {
                ListItem listItem = new ListItem();

                listItem.Created = (DateTime)row["Created"];
                listItem.CreatedBy = row["CreatedBy"] as string;
                listItem.DirName = row["DirName"] as string;
                listItem.ETagVersion = (int)row["ETagVersion"];
                listItem.FileId = new Guid(row["FileId"].ToString());
                listItem.FileName = row["FileName"]as string;
                listItem.InternalVersion = (int)row["InternalVersion"];
                listItem.SiteId = new Guid(row["SiteId"].ToString());
                listItem.Size = (int)row["Size"];
                listItem.UIVersion = (int)row["UIVersion"];
                listItem.WebId = new Guid(row["WebId"].ToString());

                result.Add(listItem);
            }

            return result;
        }
    }
}
